<?php
	session_start();
	$user=$_SESSION["user"];
	include("../connections/agriculture.php");
	if($_POST["op"] == "上    传") {
	function fileExtName ($fStr) {
		$retval = "";
		$pt = strrpos($fStr, ".");
		if ($pt) $retval = substr($fStr, $pt+1, strlen($fStr) - $pt);
		return ($retval);
	}	
	$uploaddir = './images/';
	$ext = fileExtName($_FILES['photo']['name']);
	$ext = strtolower($ext);
	if($ext!="jpg" && $ext!="gif") {
		die("文件类型出错，请返回重试。");
	}
	$uploadfile = $uploaddir . $_SESSION["user_name"].".".$ext;
	echo $uploadfile;
	if (move_uploaded_file($_FILES['photo']['tmp_name'], $uploadfile)) {
		$id = $_SESSION["userid"];
		$sql = "update users set user_photo='{$uploadfile}' where user_name='$user' ";
		if(mysql_query($sql))  {
			echo "上传成功！";
			//header("Location:msg.php?m=upload_success");
		}else {
			die("数据库错误，请重试");
		}
	} else {
		die("上传失败，请返回重试。");
	}
	}
	$sqlq = "SELECT * FROM users WHERE user_name = '$user'";
	$res = mysql_query($sqlq);
	$row = mysql_fetch_array($res);
	$photo = $row["user_photo"];
?>
<style type="text/css">
<!--
.STYLE1 {
	color: #FF0000;
	font-weight: bold;
}
-->
</style>
<body><table width="500" border="0" align="center">
  <tr>
    <td height="19" align="center"><p><span class="STYLE3">上传照片</span></p>
      <p><img src="<?php echo $photo; ?>" alt="" width="128" height="128" /></p>    </td>
  </tr>
  <tr>
    <td height="19" align="center"><form name="photo" method="post" action="photo.php" enctype="multipart/form-data" >
      <span class="STYLE1">（请上传格式为jpg或者gif的照片）</span>
      <input type="file" name="photo" size="25" />
	<input type="submit" name="op" value="上    传" /></form></td>
  </tr>
 
</table>

